package cn.com.clearbus.builder;


import cn.com.clearbus.entity.weChat.msg.TextMsg;
import cn.com.clearbus.entity.weChat.msg.WeChatMsg;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

public class FormatBuilder extends AbstractBuilder{

    private AbstractBuilder builder;

    private WeChatMsg wechatMsg;

    public FormatBuilder(AbstractBuilder builder,WeChatMsg wechatMsg){
        this.builder = builder;
        this.wechatMsg = wechatMsg;
    }

    @Override
    public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage, WxMpService service) {
        content = prebuid();
        return builder.build(content,wxMessage,service);
    }


    private String prebuid(){
        StringBuilder result = new StringBuilder();
        TextBuilder textBuilder = builder instanceof TextBuilder ? ((TextBuilder) builder) : null;
        if (textBuilder != null) {
            TextMsg<String> textMsg = wechatMsg instanceof TextMsg ? ((TextMsg) wechatMsg) : null;
            if (textMsg != null) {
                for (int i = 0; i < textMsg.getNodeList().size(); i++)
                    result.append(textMsg.getNodeList().get(i).getModelView());
            }
        }
        this.logger.info("当前格式化的消息为:{}",result.toString());
        return result.toString();
    }

}
